
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// Right_Menu Data Class
    /// </summary>
    public partial class Right_MenuDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Right_Menu";

        public Right_MenuDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Right_Menu model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [Right_Menu](");
            strSql.Append("[MenuID],[MenuParentId],[MenuName],[MenuNameEn],[MenuURL],[MenuImage],[Sort],[Remark],[Type]");
            strSql.Append(") values (");
            strSql.Append("@MenuID,@MenuParentId,@MenuName,@MenuNameEn,@MenuURL,@MenuImage,@Sort,@Remark,@Type");
            strSql.Append(")");

            SqlParameter[] parameters = {
                    new SqlParameter("@MenuID", model.MenuID),
                    new SqlParameter("@MenuParentId", model.MenuParentID),
                    new SqlParameter("@MenuName", model.MenuName),
                    new SqlParameter("@MenuNameEn", model.MenuNameEn),
                    new SqlParameter("@MenuURL", model.MenuURL),
                    new SqlParameter("@MenuImage", model.MenuImage),
                    new SqlParameter("@Sort", model.Sort),
                    new SqlParameter("@Remark", model.Remark),
                    new SqlParameter("@Type", (int)model.Type)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Right_Menu model, Int32 menuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Right_Menu] set ");
            strSql.Append("[MenuID]=@MenuID,[MenuParentId]=@MenuParentId,[MenuName]=@MenuName,[MenuNameEn]=@MenuNameEn,[MenuURL]=@MenuURL,[MenuImage]=@MenuImage,[Sort]=@Sort,[Remark]=@Remark,[IsEnabled]=@IsEnabled,ModifiedTime=@ModifiedTime");
            strSql.Append(" where [MenuID]=@OldMenuID");
            strSql.Append(" update Right_Menu set [MenuParentId]=@MenuID where MenuParentId=@OldMenuID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@OldMenuID", menuID),
                    new SqlParameter("@MenuID", model.MenuID),
                    new SqlParameter("@MenuParentId", model.MenuParentID),
                    new SqlParameter("@MenuName", model.MenuName),
                    new SqlParameter("@MenuNameEn", model.MenuNameEn),
                    new SqlParameter("@MenuURL", model.MenuURL),
                    new SqlParameter("@MenuImage", model.MenuImage),
                    new SqlParameter("@Sort", model.Sort),
                    new SqlParameter("@Remark", model.Remark),
                    new SqlParameter("@ModifiedTime", model.ModifiedTime),
                    new SqlParameter("@IsEnabled", model.IsEnabled)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(int MenuID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Right_Menu] ");
            strSql.Append(" where [MenuID]=@MenuID");

            SqlParameter[] parameters = {
            
                new SqlParameter("@MenuID", MenuID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_Menu Get(int MenuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Right_Menu] ");
            strSql.Append(" where [MenuID]=@MenuID");

            SqlParameter[] parameters = {
            
                new SqlParameter("MenuID", MenuID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_Menu Get(DataRow row)
        {
            if (row == null) return null;

            Right_Menu model = new Right_Menu();


            if (row["MenuID"] != DBNull.Value)
            {
                model.MenuID = (int)row["MenuID"];
            }

            if (row["MenuParentId"] != DBNull.Value)
            {
                model.MenuParentID = (int)row["MenuParentId"];
            }

            if (row["MenuName"] != DBNull.Value)
            {
                model.MenuName = (String)row["MenuName"];
            }

            if (row["MenuNameEn"] != DBNull.Value)
            {
                model.MenuNameEn = (String)row["MenuNameEn"];
            }

            if (row["MenuURL"] != DBNull.Value)
            {
                model.MenuURL = (String)row["MenuURL"];
            }

            if (row["MenuImage"] != DBNull.Value)
            {
                model.MenuImage = (String)row["MenuImage"];
            }

            if (row["Sort"] != DBNull.Value)
            {
                model.Sort = (Int32)row["Sort"];
            }

            if (row["Remark"] != DBNull.Value)
            {
                model.Remark = (String)row["Remark"];
            }

            if (row["Type"] != DBNull.Value)
            {
                model.Type = (SiteType)(Int32)row["Type"];
            }

            if (row["IsEnabled"] != DBNull.Value)
            {
                model.IsEnabled = (Boolean)row["IsEnabled"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(SiteType type, string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat("select * from [Right_Menu] where [Type]={0}", (int)type);

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" and " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取子菜单的最大ID
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public int GetMaxChild(int MenuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat("select isnull(max(MenuID),0) from [dbo].[Right_Menu] where MenuParentID={0}", MenuID);

            return (int)DbHelperSQL.GetSingle(strSql.ToString());
        }
        #endregion  成员方法
    }
}


